/**
 * Created by bobby on 2016/3/15.
 */

'use strict';

var formateNowTime = function () {
    var date = new Date();
    return date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
}

var formatStatus = function () {
    return "正常";
}

var getRealTimeData = function () {
    var arrays = [];
    for (var i = 0; i < 10; i++) {
        var record = {};
        record.sensorId = 1000 + i;
        record.sensorLocation = 107 + i + "回风巷";
        record.o2 = (Math.random() * 100).toFixed(2);
        record.co = (Math.random() * 100).toFixed(2);
        record.ch4 = (Math.random() * 100).toFixed(2);
        record.temp = (Math.random() * 100).toFixed(2);
        record.humidity = (Math.random() * 100).toFixed(2);
        record.diffpa = (Math.random() * 100).toFixed(2);
        record.electric = (Math.random() * 100).toFixed(2);
        record.time = formateNowTime();
        record.status = formatStatus();
        record.desc = '暂无备注';
        arrays.push(record);
    }
    console.log(arrays);
    return arrays;
}

angular.module('main.realTimeList', ['ngRoute'])
    .config(['$routeProvider', function ($routeProvider) {
        $routeProvider.when('/realTimeList', {
            templateUrl: 'realTimeList.html',
            controller: 'RealTimeController'
        });
    }])
    .service('getRealTimeRecords', function () {
        return getRealTimeData();
    }).controller('RealTimeController', function ($scope, getRealTimeRecords) {
        $scope.records = getRealTimeRecords;
        window.setInterval(function(){
            console.log("------");
            $scope.$apply(function () {
                $scope.records = getRealTimeData();
            });
        },1000);

        $scope.onItemclick = function(obj,eventId) {
            console.log(obj+"#"+eventId);

            switch (eventId){
                case 1 : $("#columnSettingModal").modal('show');break;
                default :
                    $("#columnChartModal").modal('show');
                    $("#chartRegion").load("innerLinechart.html");
                    break;
            }
        }

        $(".table_style tr").each(function () {
            $(this).children("td:eq(0)").click(function () {
                $("#columnSettingModal").modal('show');
            });
        });
        $(".table_style tr").each(function () {
            $(this).children("td:eq(2)").click(function () {
                $("#columnChartModal").modal('show');
                $("#chartRegion").load("innerLinechart.html");
            });
        });
    });

